1. /* sdmput.cpp by K.Tsuru */
  2. // function ID = 344 DRADIX
  3. /*****************************************************************
  4. SDouble class
  5. It provides the output.
  6. In order to decrease the code size, no function of iostream class
  7. is used.
  8. In header "sdbl.h" the function prototype is given below
  9. --------
  10. long Put(long figUnit=5, long pf = 0, int pL=0, int lF = CRLF|ROUND|INT_PUT, int dm=' ') const;
  11. long Puts(long figUnit=5,long pf = 0, int pL=0, int lF = CRLF|ROUND|INT_PUT, int dm=' ') const;
  12. --------
  13. output format of SDouble
  14. 1.234567890 1234567890 1234567890 1234567890 1234567890 ( 50)
  15. <- fU=10 ->
  16. <---------------- perLine = 5 units -----------------> (SHOW_FIG)
  17. 1) If figUnit == 0, it continuously outputs with neither delimiter nor carridge return.
  18. 2) putFig : the number of decimal figures. default : putFig = 0 all figures
  19. 3) perLine = the number of blocks which contains "fig" decimal per line
  20. default : perLine = 0 automatically set perLine = displayWidth/(fig+1)
  21. 4) lF(line format) : NO_CR, CRLF, CONTINUE and END_CR have same meaning as SLong class's ones.
  22. ROUND : do Round() or not.
  23. INT_PUT : It outputs a decimal figure as integer part.
  24. SHOW_FIG: It shows the number of decimal figures at end of line and adds carridge return.
  25. 5) Insert delimiter given by "delmt" at intervals of "fig(Unit)" characters.
  26. If delmt == 0, no delimiter is inserted.
  27. It returns the total number of output bytes, including '\n', '.' and delimiter,
  28. as "printf()".
  29. ******************************************************************/
  30. #ifndef SN_H
  31. #include "sn.h"
  32. #endif
  33. static int putcr(int cr, FILE* out){
  34. if(cr) putc('\n', out);
  35. return cr ? 1 : 0;
  36. }
  37. static int putdelmt(int d, FILE* out){
  38. if(d) putc(d, out);
  39. return d ? 1 : 0;
  40. }
  41. /**************************************************
  42. sub-function
  43. It returns (n+1)-th decimal place by a character.
  44. The mantissa begins at figure[1].
  45. ***************************************************/
  46. // function ID = 306
  47. int SDouble::CharNthDec(long n) const{
  48. static int pos = -1;
  49. static char buff[DFIGURES+1];
  50. int p, r;
  51. if(n < 0){ pos = -1; return 0; } //initialize
  52. p = int(n/DFIGURES) +1; //in figure[p]
  53. r = int(n%DFIGURES); //r-th position in figure[p]
  54. if(p != pos){ //renew buff[]
  55. pos = p;
  56. fType f = figure(p);
  57. for(int j = DFIGURES-1; j >= 0; j--){
  58. buff[j] = '0' + f%10;
  59. f /= 10;
  60. }
  61. }
  62. return (int)buff[r];
  63. }
  64. // function ID = 344
  65. /*********************************************************
  66. main body
  67. usage : x.Put()
  68. putFig : the number of output figures
  69. fig : distance of delimiter
  70. delmt : delimiter, default is a space ' '
  71. It returns the total number of bites of output characters.
  72. **********************************************************/
  73. //long SDouble::Put(long fig, long putFig, int perLine, int lF, int delmt) const{
  74. long SDouble::Put(long fU, long pF, int pL, int lF, int dm) const{
  75. SignCheck(344); // sign == UNDECIDED ?
  76. FILE* out = FileStream();
  77. if(fU > 0) {
  78. figUnit = fU; putFig = pF; perLine = pL; lineFormat = lF; delmt = dm;
  79. } else if(fU == 0){ putFig = LONG_MAX; delmt = 0; } // continuously output with no delimiter
  80. // else uses current format
  81. int showFig = lineFormat & SHOW_FIG;
  82. int crlf = lineFormat & END_CR;
  83. if( Sign(344) == 0 ){
  84. putc('0',out); putc('.',out); putc('0',out); return 3+putcr(crlf, out);
  85. }
  86. SDouble temp(*this);
  87. long cCount = 0;
  88. char buff[displayWidth+1];
  89. int nc = 0; //the number of characters in a line
  90. long decCount = 0; //0.abcd|efgh|.... counter of decimal part
  91. temp.StdReform(344); //standard form
  92. // lF temp display initial value of decCount
  93. // lF & INT_PUT 0|000a|bcde|fgh..... a.bcdefgh... DFIGURES
  94. // else 0|abcd|efgh|..... 0.abcdefgh.... 0
  95. uint last; //last position of output
  96. if(lineFormat & ROUND) temp.Round(); //round off
  97. long exp = DFIGURES*(long)temp.RdxExp();//exponent in a decimal system
  98. ulong v = (ulong)temp.figure(1);
  99. #ifndef NDEBUG
  100. assert(v);
  101. #endif
  102. //It reforms into "0.abcd ...".
  103. int k = 0;
  104. while(v < DRADIX){ v *= 10; k++; }
  105. k--;
  106. temp.TempPointFree(); //temporally into floating point out is changed ?
  107. if(k > 0) {
  108. v = (ulong)ipow10(k); temp = DsMult(temp, v); exp -= k;
  109. }
  110. if(lineFormat & INT_PUT){
  111. //In order to display such as "a.bcdef...." it multiplies by ten.
  112. if(putFig > 1) putFig--;
  113. temp = DsMult(temp, 10); exp--;
  114. decCount = DFIGURES;
  115. }
  116. temp.PointModePop();
  117. if(lineFormat & ROUND) last = min(temp.Last(), temp.EffFig()); //do not display hidden figures
  118. else last = temp.Last();
  119. //the number of decimal figures between decCount/DFIGURES and last
  120. long decfig = long(last - decCount/DFIGURES)*(long)DFIGURES; //maybe last=1
  121. if(putFig > 0) decfig = min(decfig, putFig);
  122. //display the negative sign
  123. if( temp.Sign(344) == -1 ){ putc('-', out); cCount++; }
  124. //display the integral part
  125. buff[0] = '0'; buff[1] = '\0'; // buff[] = "0"
  126. if(decCount) buff[0] += temp.figure(1);
  127. if( (decfig + decfig/figUnit) > (int)displayWidth ){
  128. strcat(buff, ".+");
  129. if(lineFormat & CONTINUE) strcat(buff, "\\");
  130. cCount += fprintf(out, "%s\n",buff); //over one line
  131. } else {
  132. cCount += fprintf(out, "%s.",buff); //show point
  133. nc = (int)cCount;
  134. }
  135. //decimal part
  136. int rest; //rest of characters at last line
  137. int figWidth; //the width of figure display at the end of line, except ()
  138. int dec_init = (int)decCount;
  139. if(!decfig){ //decimal part=0
  140. putc('0', out); cCount++;
  141. rest = displayWidth - (int)cCount;
  142. } else {
  143. //non zero decimal part
  144. int putdec = 0, crLine, figCount = 0;
  145. figWidth = iFigures(decfig);
  146. crLine = (perLine > 0 ) ? perLine : int( (displayWidth-1)/(figUnit+1) );
  147. if(showFig) while((int)displayWidth -crLine*(figUnit+1)<(figWidth+3) ) crLine--;
  148. if( crLine<=0 ) crLine = 1;
  149. temp.CharNthDec(-1L);
  150. while(decfig){
  151. putc( temp.CharNthDec(decCount), out);
  152. decfig--; cCount++; decCount++; nc++; putdec++;
  153. if(putdec == figUnit){
  154. figCount++; nc++; putdec = 0;
  155. if( figCount != crLine ) cCount += putdelmt(delmt, out);
  156. }
  157. if( figCount == crLine ){ //reaches at line end
  158. if(showFig) cCount += fprintf(out, " (%*ld)", figWidth, decCount - dec_init);
  159. if(delmt != ' ') cCount += putdelmt(delmt, out);
  160. if(lineFormat & CONTINUE){ putc('\\', out); cCount++; }
  161. if(lineFormat & (CRLF|SHOW_FIG)){
  162. putc('\n', out); cCount++;
  163. }
  164. figCount = 0; nc = 0;
  165. }
  166. }
  167. rest = int((figUnit+1)*crLine -nc);
  168. }
  169. //display exponent part
  170. if(exp){
  171. cCount += sprintf(buff,"e%+ld",exp);
  172. if(rest < (int)strlen(buff) ){
  173. if(lineFormat & CONTINUE){ putc('\\', out); cCount++; }
  174. putc('\n', out); cCount++; rest = displayWidth;
  175. }
  176. rest -= fprintf(out, "%s", buff);
  177. }
  178. //display the number of figures at the last line
  179. if(showFig && nc && (decCount - dec_init) ){//When nc=0 the display ended.
  180. sprintf(buff,"(%ld)", decCount - dec_init);
  181. cCount += fprintf(out, "%*s%s",rest," ", buff);
  182. }
  183. cCount += putcr(crlf, out);
  184. return cCount;
  185. }

sdmput.cpp : last modifiled at 2017/08/21 12:10:30(7,197 bytes)
created at 2017/10/07 10:21:15
The creation time of this html file is 2017/10/07 10:30:03 (Sat Oct 07 10:30:03 2017).